<?php
namespace Modules\Admin\Controllers;

class ControllerBase extends \Phalcon\Mvc\Controller {
  use \Common\Cache\BackendCacheTrait;

  protected $admin = array();

  public function checkToken() {
    $request = $this -> request;
    $token = null;
    if($request -> isGet()) {
      $token = $request -> get('token', 'string', null, true);
    } else if($request -> isPost()) {
      $token = $request -> post('token', 'string', null, true);
    }

    $response = $this -> response;
    if(!$token) {
      $response -> setStatusCode('401') -> setJsonContent(['code' => -1, 'message' => 'Unauthorized TOKEN!']) -> send();
      exit;
    }

    $checkTokenJWT = (new \Modules\Admin\Libraries\JWT()) -> checkToken($token);
    if($checkTokenJWT['code'] != 0) {
      $response -> setStatusCode('401') -> setJsonContent(['code' => $checkTokenJWT['code'], 'message' => $checkTokenJWT['message']]) -> send();
      exit;
    }

    if(!$checkTokenJWT || !$checkTokenJWT['data'] || !$checkTokenJWT['data']['admin']) {
      $response -> setStatusCode('401') -> setJsonContent(['code' => -1, 'message' => '未知错误!']) -> send();
      exit;
    }

    $this -> admin = $checkTokenJWT['data']['admin'];
  }
}
?>